package cn.th.controller;


import cn.th.common.R;
import cn.th.entity.Gift;
import cn.th.entity.Item;
import cn.th.service.IGiftService;
import cn.th.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 * 物品表（任务所需的东西，不限于实物） 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 新增物品
     * @param item
     * @return
     */
    @PostMapping
    public R<?> save(@RequestBody Item item) {
        log.info("gift:{}", item);
        itemService.save(item);
        return R.success(item,"新增物品成功");
    }

    /**
     * 查询物品表
     * @param page
     * @param pageSize
     * @param school
     * @param name
     * @param attribute
     * @return
     */
    @GetMapping("/page")
    public R<Map<String,Object>> page(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "pageSize") int pageSize,
                                      @RequestParam(value = "school", required = false) String school,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "attribute", required = false) String attribute){

        Map<String,Object> data = itemService.queryItem(page, pageSize, school, name,attribute);

        return R.success(data,"查询礼品表成功");
    }

    /**
     * 根据id删除物品表
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除物品表信息，id为：{}",id);

        //删除物品表的数据
        itemService.removeById(id);

        return R.success("删除物品表信息成功");
    }

    /**
     * 修改物品表
     * @param item
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Item item){
        log.info("修改礼品表信息：{}",item);

        itemService.updateById(item);

        return R.success("修改物品表成功");
    }

    /**
     * 修改物品时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Item> getItemById(@PathVariable("id")Long id){
        Item item = itemService.getById(id);
        return R.success(item);
    }
}

